import request from "@/modules/index/utils/request";
import qs from "qs";
import { downloadFile } from "@/utils/common";
import { Message } from "element-ui";

const store = {
    actions: {
        // 获取模型管理列表数据
        async modelManageProjectSearchPage(context, params) {
            const data = await request({
                url: "project/search-page",
                method: "post",
                data: params
            });
            return data;
        },

        // 删除模型记录
        async modelManageProjectBatchDelete(context, params) {
            const data = await request({
                url: "project/batch",
                method: "delete",
                params: params,
                paramsSerializer: params => {
                    return qs.stringify(params, { indices: false });
                }
            });
            return data;
        },

        // 创建项目
        async modelManageProject(context, params) {
            const data = await request({
                url: "project",
                method: "post",
                data: params
            });
            return data;
        },

        // 素材和项目关联
        async modelManageMaterialInfoSave(context, params) {
            const data = await request({
                url: "material-info/save",
                method: "post",
                data: params
            });
            return data;
        },

        // 创建素材和上传预测文件
        async modelManageMaterialInfoSaveAndUpload(context, params) {
            const data = await request({
                url: "material-info/save-and-upload",
                method: "post",
                data: params,
                headers: {
                    "Content-Type": "multipart/form-data"
                }
            });
            return data;
        },

        // 获取数据集列表
        async modelManageMaterialInfoSearch(context, params) {
            const data = await request({
                url: "material-info/search",
                method: "post",
                data: params
            });
            return data;
        },

        // 单条查询
        async modelManageMaterialInfoFindByProjectId(context, params) {
            const data = await request({
                url: "material-info/findByProjectId",
                method: "post",
                params: params
            });
            return data;
        },

        // 根据id查询训练情况
        async modelManageTraininginfoTrainingInfoGetByProjectId(
            context,
            params
        ) {
            const data = await request({
                url: "traininginfo/training-info/getByProjectId",
                method: "get",
                params: params
            });
            return data;
        },

        // 获取模型详情
        async modelManageModelDetail(context, params) {
            const data = await request({
                url: "project/" + params.id,
                method: "GET"
            });
            return data;
        },

        // 获取模型列表
        async modelManageTrainingModelDataFindByProjectId(context, params) {
            const data = await request({
                url: "training-model-data/find-by-projectId/" + params.id,
                method: "GET"
            });
            return data;
        },

        // 获取模型预测数据
        async modelManagePredictTaskSearch(context, params) {
            const data = await request({
                url: "/predicttask/predict-task/search",
                method: "POST",
                data: params
            });
            return data;
        },

        // 预览当前模型训练的数据集
        async modelManageMaterialInfoViewFile(context, params) {
            const data = await request({
                url: "material-info/viewfile",
                method: "get",
                params: params
            });
            return data;
        },

        // 获取算法数据
        async modelManageClassifyGetAlgorithmByType(context, params) {
            const data = await request({
                url: "classify/get-algorithm-by-type",
                method: "get",
                params: params
            });
            return data;
        },

        async modelManageTrainingModelDataFindByTrainingTask(context, params) {
            const taskId = params.taskId || "";
            const data = await request({
                url: `/training-model-data/find-by-training-task/${taskId}`,
                method: "get"
            });
            return data;
        },

        // 创建训练
        async modelManageTraininginfoTrainingInfoCreate(context, params) {
            const data = await request({
                url: "/traininginfo/training-info/create",
                method: "post",
                data: params
            });
            return data;
        },

        // 开始训练
        async modelManageTraininginfoTrainingInfoStartTask(context, params) {
            const data = await request({
                url: `/traininginfo/training-info/startTask?id=${params.id}`,
                method: "post"
            });
            return data;
        },

        // 获取预测上传文件的任务id
        async modelManagePublishTaskDataSearch(context, params) {
            const data = await request({
                url: "/publishtask/publish-task-data/search", //接口
                method: "GET",
                params: { page: 1, pageSize: 1, projectId: params.projectId }
            });
            return data;
        },

        // 新建预测上传文件
        async modelManageMaterialInfoUpload(context, params) {
            const data = await request({
                url: `material-info/upload`, //接口
                method: "post",
                data: params,
                headers: { "Content-Type": "multipart/form-data" }
            });
            return data;
        },

        // 创建预测任务
        async modelManagePredictDataMultiCreate(context, params) {
            const data = await request({
                url:
                    `/publishtask/publish-task-data/` +
                    params.taskId +
                    `/predict-data-multi-create`, //接口
                method: "POST",
                data: params
            });
            return data;
        },

        // 执行预测任务
        async modelManagePredictDataMultiRun(context, params) {
            const data = await request({
                url:
                    `/publishtask/publish-task-data/` +
                    params.taskId +
                    `/predict-data-multi?page=${params.page}&pageSize=${params.pageSize}`, //接口
                method: "POST",
                data: params
            });
            return data;
        },

        // 发布模型
        async modelManagePublishtaskPublishTaskDataPublish(context, params) {
            const data = await request({
                url: "/publishtask/publish-task-data/publish",
                method: "post",
                data: params
            });
            return data;
        },

        // 下线模型
        async modelManagePublishtaskPublishTaskDataIdStop(context, params) {
            const publishTaskId = params.publishTaskId || "";
            const data = await request({
                url: `/publishtask/publish-task-data/${publishTaskId}/stop`,
                method: "post"
            });
            return data;
        },

        // 通过模型id找上线的模型
        async modelManagePublishtaskPublishTaskDataFindByModelId(
            context,
            params
        ) {
            const modelId = params.modelId || "";
            const data = await request({
                url: `/publishtask/publish-task-data/find-by-model/${modelId}`,
                method: "get"
            });
            return data;
        },

        // 模型下载
        async modelManageTrainingModelDataModelDataSDKId(context, params) {
            const data = await request({
                url: `/training-model-data/model-data-sdk-list/${params.id}`,
                method: "get",
                responseType: "blob"
            });
            if (!data.data) {
                Message.error("下载文件失败");
                return;
            }
            downloadFile(data);
        },

        // 模型预测预测单条数据
        async modelManagerPublishTaskData(context, params) {
            const data = await request({
                url:
                    "/publishtask/publish-task-data/" +
                    params.taskId +
                    "/predict-data",
                method: "post",
                data: params
            });
            return data;
        },

        // 文本预测-form表单字段加载
        async modelManagerPublishtaskPublishTaskDataIdGetPredictJson(
            context,
            params
        ) {
            const data = await request({
                url:
                    "/publishtask/publish-task-data/" +
                    params.taskId +
                    "/get-predict-json",
                method: "post",
                data: params
            });
            return data;
        },

        // 导入模型-下载模板
        async modelManageImportModelTaskDownloadModelTemplate(context, params) {
            const data = await request({
                url: "/import-model-task/downloadModelTemplate",
                method: "post",
                params: params,
                responseType: "blob"
            });
            if (!data.data) {
                Message.error("下载文件失败");
                return;
            }
            downloadFile(data);
        },

        // 下载预测模板
        async modelManageMaterialInfoDownloadPredictTemplate(context, params) {
            // projectType
            const data = await request({
                url: "/material-info/downloadPredictTemplate",
                method: "post",
                params: params,
                responseType: "blob"
            });
            if (!data.data) {
                Message.error("下载文件失败");
                return;
            }
            downloadFile(data);
        },

        // 下载预测记录
        async downloadTestResultFile(context, params) {
            // projectType
            const data = await request({
                url: "/algoPredictRecord/downloadByTaskId",
                method: "get",
                params: params,
                responseType: "blob"
            });
            if (!data.data) {
                Message.error("下载文件失败");
                return;
            }
            downloadFile(data);
        },

        // 获取设备实例
        async instanceSearchPage(context, params) {
            const data = await request({
                url: params
                    ? `equipCatalog/pageList?${params}`
                    : `equipCatalog/pageList`,
                method: "get"
            });
            // console.log()
            return data;
        },

        // 获取设备实例ById
        async instanceSearchById(context, params) {
            const data = await request({
                url: `equipEntity/getById?id=${params}`,
                method: "get"
            });
            return data;
        },

        // 删除设备实例
        async instanceDelete(context, params) {
            const data = await request({
                url: `equipEntity/delete?id=${params}`,
                method: "post"
            });
            return data;
        },

        // 批量删除设备实例
        async instanceBatchDelete(context, params) {
            const data = await request({
                url: "equipEntity/deleteByIds",
                method: "post",
                params: params,
                paramsSerializer: params => {
                    return qs.stringify(params, { indices: false });
                }
            });
            return data;
        },

        // 创建设备实例
        async instanceSave(context, params) {
            const data = await request({
                url: "equipEntity/save",
                method: "post",
                data: params
            });
            return data;
        },
        // 修改设备实例
        async instanceUpdate(context, params) {
            const data = await request({
                url: "equipEntity/update",
                method: "post",
                data: params
            });
            return data;
        },
        // 算法裁剪规则保存接口
        async postCodeCutRules(context, params) {
            const data = await request({
                url: `/algoCorpgenerate/saveOrUpdateBatch?type=${params.type}`,
                method: "post",
                data: params
            });
            return data;
        }
    }
};

export default store;
